package com.learncloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.learncloud.common.core.Constant;
import com.learncloud.entity.vo.VideoLabelVo;
import com.learncloud.entity.VideoLabel;
import com.learncloud.mapper.VideoLabelMapper;
import com.learncloud.service.IVideoLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author yj
 */
@Service
public class VideoLabelServiceImpl extends ServiceImpl<VideoLabelMapper, VideoLabel> implements IVideoLabelService {

    @Autowired
    private VideoLabelMapper videoLabelMapper;

    @Override
    public List<VideoLabelVo> getLabelVoList(VideoLabel videoLabel, Integer categoryId) {
        List<VideoLabelVo> parents = new ArrayList<>();
        List<VideoLabel> list;
        if (categoryId == null){
            list = videoLabelMapper.selectList(new QueryWrapper<>(videoLabel.setStatus(Constant.Status.ENABLE.value)));
        }else {
            list = videoLabelMapper.getLabelVoList(videoLabel,categoryId);
        }
        for (VideoLabel item : list){
            if (item.getParentId().equals(0)){
                VideoLabelVo parent = new VideoLabelVo(item);
                getChild(parent,list);
                parents.add(parent);
            }
        }
        return parents;
    }

    @Override
    public Page getPaginationList(Page page, VideoLabel videoLabel) {
        // 分页查询父级菜单
        videoLabel.setParentId(0);
        page.setOrders(Arrays.asList(new OrderItem().setColumn("sort").setAsc(true)));
        page = this.page(page, new QueryWrapper<>(videoLabel));
        List<VideoLabel> records = page.getRecords();
        List<VideoLabelVo> parents = new ArrayList<>();
        // 通过父级id批量递归查询所有子级
        if (records.size() > 0) {
            List<Integer> list = new ArrayList<>();
            for (VideoLabel item : records){
                list.add(item.getId());
            }
            List<VideoLabel> recursionByParentIds = videoLabelMapper.findByParentIds(list);
            for (VideoLabel menu : records){
                VideoLabelVo parent = new VideoLabelVo(menu);
                getChild(parent,recursionByParentIds);
                parents.add(parent);
            }
            page.setRecords(parents);
        }
        return page;
    }

    private void getChild(VideoLabelVo videoLabelVo, List<VideoLabel> list){
        for (VideoLabel item : list){
            if (videoLabelVo.getId().equals(item.getParentId())){
                if (videoLabelVo.getChildren() == null){
                    videoLabelVo.setChildren(new ArrayList<>());
                }
                videoLabelVo.getChildren().add(item);
            }
        }
    }
}
